const mutations = {
  // 设置播放列表
  setSongList(state, list) {
    state.songList = []
    if (!(list instanceof Array)) {
      list = [list]
    }
    // 过滤列表
    list.forEach(item => {
      if (item.fee === 0 || item.fee === 8) {
        state.songList.push(item)
      }
    })
    state.currentSong = state.songList[state.playerSetting.index]
      ? state.songList[state.playerSetting.index]
      : {}
    window.localStorage.setItem('songList', JSON.stringify(state.songList))
  },
  // 追加音乐到播放列表
  pushSongList(state, list) {
    const songidList = []
    state.isPlay = false
    state.songList.forEach(item => {
      songidList.push(item.id)
    })
    if (list instanceof Array) {
      list.forEach(item => {
        // if (item.fee === 0 || item.fee === 8) {
        //   if (!songidList.includes(item.id)) {
        //     state.songList.push(item)
        //   }
        // }
        if (!songidList.includes(item.id) && (item.fee === 0 || item.fee === 8)) {
          state.songList.push(item)
        }
      })
    } else {
      if (!songidList.includes(list.id) && (list.fee === 0 || list.fee === 8)) {
        state.songList.push(list)
      }
    }
    window.localStorage.setItem('songList', JSON.stringify(state.songList))
  },
  // 移除歌曲
  popSongList(state, id) {
    state.isPlay = false
    const currentIndex = state.playerSetting.index
    const index = state.songList.findIndex(item =>  item.id ===id )
    if (index < currentIndex) {
      state.playerSetting.index -= 1
    }
    state.songList.splice(index,1)
  },
  // 清空播放列表
  clearSongList(state) {
    state.songList = []
    // state.playerSetting.index = 0
    window.localStorage.setItem('songList', JSON.stringify(state.songList))
  },
  // 页面刚加载时初始播放列表
  initSongList(state) {
    state.isPlay = false
    if (window.localStorage.getItem('songList')) {
      state.songList = JSON.parse(window.localStorage.getItem('songList'))
    } else {
      window.localStorage.setItem('songList', JSON.stringify(state.songList))
    }
    state.currentSong = state.songList[state.playerSetting.index]
      ? state.songList[state.playerSetting.index]
      : {}
  },
  // 是否直接播放音乐
  isPlayTrue(state) {
    state.isPlay = true
  },
  isPlayFalse(state) {
    state.isPlay = false
  },
  // 初始化播放设置
  initPlayerSetting(state) {
    console.log('init-index')
    state.isPlay = false
    if (window.localStorage.getItem('player-setting') !== null) {
      state.playerSetting = JSON.parse(window.localStorage.getItem('player-setting'))
    } else {
      window.localStorage.setItem('player-setting', JSON.stringify(state.playerSetting))
    }
  },
  // 设置播放设置-当前播放音乐
  setplayerSettingIndex(state, index) {
    console.log('test1222', index)
    state.isPlay = true
    if (index < 0) {
      state.playerSetting.index = state.songList.length + index
    } else {
      state.playerSetting.index = index
    }
    state.currentSong = state.songList[index] ? state.songList[index] : {}
    window.localStorage.setItem('player-setting', JSON.stringify(state.playerSetting))
  },
  // 设置播放设置-播放模式
  setplayerSettingMode(state, mode) {
    state.playerSetting.mode = mode
    window.localStorage.setItem('player-setting', JSON.stringify(state.playerSetting))
  },
  // 设置播放设置-音量
  setplayerSettingVol(state, vol) {
    state.playerSetting.volume = vol
    window.localStorage.setItem('player-setting', JSON.stringify(state.playerSetting))
  },
  setIndexChanging(state, boolean) {
    state.indexChanging = boolean
  },
  // 设置当前歌曲
  setCurrentSong(state, song) {
    state.currentSong = song
  },
  setListDisplay(state, boolean) {
    state.listDisplay = boolean
  }
}
export default mutations
